package com.dsteiner.gwt.cafeteriasophia.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.gwt.cafeteriasophia.client.domain.Sale;
import com.dsteiner.gwt.cafeteriasophia.client.model.SaleLineModel;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.ObjectUtil;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;

public class SaleLineController {

	private SaleLineModel saleLineModel;
	private PrefilledTextBoxModel salesTextBoxModel;
	private PrefilledTextBoxModel priceTextBoxModel;
	private IWidget salesPricePanel;

	public void registerPriceTextBoxModel(final PrefilledTextBoxModel priceTextBoxModel) {
		this.priceTextBoxModel = priceTextBoxModel;
		priceTextBoxModel.addPropertyChangeListener("hasFocus", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				if(!priceTextBoxModel.hasFocus()){
					String text = priceTextBoxModel.getText();
					if(priceTextBoxModel.isEdited() && !ObjectUtil.isDouble(text)){
						JsUtil.getInstance().showAlert("ingrese un numero (por ejemplo 4.5)");
						priceTextBoxModel.setText(null);
						return;
					}
					Sale sale = saleLineModel.getSale();
					sale.setPrice(priceTextBoxModel.isEdited() ? new Double(text) : 0);
				}
			}
		});
	}

	public void registerSaleLineModel(SaleLineModel saleLineModel) {
		this.saleLineModel = saleLineModel;
		saleLineModel.addPropertyChangeListener("sale", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateSale();
			}
		});
	}

	public void registerSaleLineWidget(ILazyWidget saleLineWidget) {
		saleLineWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateSale();
			}
		});
	}

	public void registerSalesPricePanel(IWidget salesPricePanel) {
		this.salesPricePanel = salesPricePanel;
	}

	public void registerSalesTextBoxModel(final PrefilledTextBoxModel salesTextBoxModel) {
		this.salesTextBoxModel = salesTextBoxModel;
		salesTextBoxModel.addPropertyChangeListener("hasFocus", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				if(!salesTextBoxModel.hasFocus()){
					String text = salesTextBoxModel.getText();
					if(salesTextBoxModel.isEdited() && !ObjectUtil.isDouble(text)){
						JsUtil.getInstance().showAlert("ingrese un numero (por ejemplo 4.5)");
						salesTextBoxModel.setText(null);
						return;
					}
					Sale sale = saleLineModel.getSale();
					sale.setAmount(salesTextBoxModel.isEdited() ? new Double(text) : 0);
				}
			}
		});
	}
	private void updateSale() {
		Sale sale = saleLineModel.getSale();
		salesTextBoxModel.setText(sale.getAmount() > 0 ? sale.getAmount()+"" : null);
		priceTextBoxModel.setText(sale.getPrice() > 0 ? sale.getPrice()+"" : null);
		
		Double defaultPrice = sale.getProduct().getDefaultPrice();
		salesPricePanel.setVisible(defaultPrice != null);
	}
}